package com.jymf.util;

import com.jymf.model.TreeVo;

import java.util.ArrayList;
import java.util.List;

/**
 * 下拉树工具类
 * @ClassName: TreeUtil
 * @Description: TODO
 * @author zyj
 */
public class TreeUtil {

	/**
	 * 根据父节点或取树
	 * @param list
	 * @param parsent
	 * @return  
	 * @return: List<TreeVo>
	 * @author zyj
	 */
	public static List<TreeVo> convertTreeVo(List<TreeVo> list, String parsent){
			List<TreeVo> treeList = new ArrayList<TreeVo>();
			
			if(CommonUtils.isBlank(list)){
				return treeList;
			}
			
            //循环遍历树查询的所有节点
            for (int i=(list.size()-1); i>=0 ; i--) {
            	TreeVo node = list.get(i);
                if(CommonUtils.isEmpty(node.getParentId())){
                	list.remove(node);
                	treeList.add(sortTree(list,node));//放置二级节点
                }else if(parsent!=null&&CommonUtils.isEmpty(node.getParentId())){
                	list.remove(node);
                }else if(parsent!=null&&node.getId().equals(parsent)){
                	list.remove(node);
                	treeList.add(sortTree(list,node));
                }
            }    
            
	        return treeList ;
		}
		
	 private static TreeVo sortTree(List<TreeVo> list, TreeVo rootVO) {
		 if(CommonUtils.isBlank(list)){
				return rootVO;
			}
			
			for(TreeVo vo : list){
				if(rootVO.getId().equals(vo.getParentId())){
					rootVO.getChildren().add(sortTree(list, vo));
				}
			}
			
			return rootVO;
    }
	
}
